package kevin.framework.basic.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import kevin.framework.basic.common.ui.AjaxResult;
import kevin.framework.basic.domain.Func;
import kevin.framework.basic.domain.Role;
import kevin.framework.basic.domain.RoleFunc;
import kevin.framework.basic.service.IBaseService;
import kevin.framework.basic.service.IFuncService;
import kevin.framework.basic.service.IMenuService;
import kevin.framework.basic.service.IRoleFuncService;
import kevin.framework.basic.service.IRoleService;

/**
 * Generate by Kevin's CodeBuilder
 **/
@Controller
@RequestMapping(value = "/role")
public class RoleController extends ResetfulBaseController<Role, String> {

    @Resource(name = "funcService")
    IFuncService funcService;

    @Resource(name = "roleFuncService")
    IRoleFuncService rfService;

    @Resource(name = "menuService")
    IMenuService menuService;

    IRoleService roleService;

    @Resource(name = "roleService")
    @Override
    public void setService(IBaseService<Role, String> service) {
        this.service = service;
        roleService = (IRoleService)service;
    }

    /*
     * 页面 views/base/entityPath 文件夹路径
     * ***/
    @Override
    public String setPageModelPath(HttpServletRequest request) {
        // TODO Auto-generated method stub
        return "base" + File.separator + entityPath;
    }

    @Override
    protected void onRequestPage(String pageName, HttpServletRequest request) throws Exception {
        if ("funcMrg".equals(pageName)) {
            JSONArray menuJson = menuService.getMenuJsonByPid("0");
            request.setAttribute("menuJson", menuJson);
        }
        super.onRequestPage(pageName, request);
    }

    @RequestMapping(value = "/loadFunc", method = RequestMethod.POST)
    public @ResponseBody AjaxResult loadFunc(HttpServletRequest request) throws Exception {

        String idList = request.getParameter("idList");
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> menuIds = new ArrayList<String>();

        if (idList.contains(",")) {
            String[] tmp = idList.split(",");
            for (int i = 0, len = tmp.length; i < len; ++i) {
                menuIds.add(tmp[i]);
            }
        } else if (StringUtils.isNotEmpty(idList)) {
            menuIds.add(idList);
        }
        if (menuIds.size() > 0) {
            params.put("menuIds", menuIds);
        }

        Map<String, String> order = new HashMap<String, String>();
        order.put("menu_id", "asc");
        params.put("orderColumn", order);
        List<Func> funcList = funcService.selectParam(params);

        String roleId = request.getParameter("roleId");
        Map<String, Object> roleFuncPrs = new HashMap<String, Object>();
        roleFuncPrs.put("roleId", roleId);
        if (menuIds.size() > 0) {
            roleFuncPrs.put("menuIds", menuIds);
        }
        List<RoleFunc> rfList = rfService.selectParam(roleFuncPrs);
        JSONObject rRobj = new JSONObject();
        for (RoleFunc rf : rfList) {
            rRobj.put(rf.getFuncId(), true);
        }
        JSONObject resObj = new JSONObject();
        resObj.put("funcList", funcList);
        resObj.put("rRobj", rRobj);
        return returnSuccData(resObj);
    }

    // 功能管理
    @RequestMapping(value = "/funcMrg", method = RequestMethod.POST)
    public @ResponseBody AjaxResult funcMrg(HttpServletRequest request) throws Exception {

        String roleId = request.getParameter("roleId");
        String funcs = request.getParameter("funcs");
        String menus = request.getParameter("menus");
        List<RoleFunc> list = new ArrayList<RoleFunc>();
        if (StringUtils.isNotEmpty(funcs)) {
            list = JSONArray.parseArray(funcs, RoleFunc.class);
        }
        rfService.setRoleFunc(roleId, list, menus);
        return this.returnSuccess();

    }
}
